package onlineslideshow.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

/**
 *
 * @author alex.nebel
 */
public class ftpSettings {

    private static final String[] atts = {"host", "port", "user", "pass"};
    private static final String folder = "config";
    private String filename;

    private HashMap<String, String> settings = new HashMap<String, String>();

    public ftpSettings(String filename) throws IOException{
        this.filename = "ftp-config-"+filename+(filename.endsWith(".xml")?"":".xml");
        readFtpSettings();
    }

    public void readFtpSettings() throws IOException{
        clearSettings();
        File file = new File("./"+folder+"/"+filename);
        if(file.exists()){
            FileInputStream is = new FileInputStream(file);
            Properties props = new Properties();
            props.loadFromXML(is);
            for(String key : atts){
                settings.put(key, props.getProperty(key));
            }
        }else{
            file.createNewFile();
            writeFtpSettings();
        }
    }

    public void writeFtpSettings() throws IOException {
        FileOutputStream os = null;
        os = new FileOutputStream(new File("./"+folder+"/"+filename));
        Properties props = new Properties();
        for(String key : atts){
            if(settings.get(key)==null)
                settings.put(key, "");
            props.setProperty(key, settings.get(key));
        }
        props.storeToXML(os, "ftp-setting file");
        os.flush();
        os.close();
    }

    public void clearSettings(){
        settings.clear();
        for(String key : atts){
            settings.put(key, "");
        }
    }


    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getHost() {
        return settings.get("host");
    }

    public void setHost(String host) {
        this.settings.put("host", host);
    }

    public String getPass() {
        return settings.get("pass");
    }

    public void setPass(String pass) {
        this.settings.put("pass", pass);
    }

    public String getPort() {
        return settings.get("port");
    }

    public void setPort(String port) {
        this.settings.put("port", port);
    }

    public String getUser() {
        return settings.get("user");
    }

    public void setUser(String user) {
        this.settings.put("user", user);
    }

    
}
